<template>
  <div class="post-detail-page">
    <modal title="删除文章" :visible="modalIsVisible"
      @modal-on-close="modalIsVisible=false"
      @modal-on-confirm="hideAandDelete"
    >
      <!-- 放在modal.vue中的插槽里 -->
      <p>确定要删除这篇文章？</p>
    </modal>
    <article class="w-75 mx-auto mb-5 pb-3" v-if="currentPost">
      <div class="user-profile-component border-top border-bottom py-3 mb-5 align-items-center row g-0">
        <div class="col">
          <user-profile :user="currentPost.author" v-if="typeof currentPost.author === 'object'"></user-profile>
        </div>
        <span class="text-muted col text-right font-italic">发表于：{{currentPost.createdAt}}</span>
      </div>
      <img :src="currentImageUrl" alt="currentPost.title" class="rounded-lg img-fluid my-4" v-if="currentImageUrl">
      <h2 class="mb-4">{{currentPost.title}}</h2>
      <div v-html="currentHTML"></div>
      <div v-if="showEditArea" class="btn-group mt-5">
        <router-link 
          type="button" 
          class="btn btn-success"
          :to="{name:'create',query:{id:currentPost._id}}"
          >编辑</router-link>
        <button type="button" class="btn btn-danger" @click.prevent="modalIsVisible=true">删除</button>
      </div>
    </article>
  </div>
</template>

<script lang="ts">
import { defineComponent, onMounted, computed, ref } from 'vue'
import MarkdownIt from 'markdown-it'
import { useStore } from 'vuex'
import { useRoute, useRouter} from 'vue-router'
import { GlobalDataProps, PostProps, ImageProps, UserProps, ResponseType } from '../store'
import UserProfile from '../components/UserProfile.vue'
import Modal from '../components/Modal.vue'
// import Modal from '../components/Modal.vue'
import createMessage from '../components/createMessage'

export default defineComponent({
  name: 'post-detail',
  components: {
    UserProfile,  //在文章详情页中展示用户信息的组件
    Modal   //删除提示框组件
  },
  setup() {
    const store = useStore<GlobalDataProps>()
    const route = useRoute()
    const router = useRouter()
    const modalIsVisible = ref(false) //控制提示框是否显示，默认不显示
    const currentId = route.params.id //取到文章的id
    const md = new MarkdownIt()
    onMounted(() => {
      store.dispatch('fetchPost', currentId)  //获取文章详情信息
    })
    const currentPost = computed<PostProps>(() => store.getters.getCurrentPost(currentId))  //通过文章id找到当前点击的文章对象
    const currentHTML = computed(() => {  //将输入的文本转换成html格式
      const { content, isHTML } = currentPost.value
      if (currentPost.value && content) {
        return isHTML ? content : md.render(content)  //????
      }
      return null
    })
    const showEditArea = computed(()=>{ //是否展示编辑删除区域
      const { isLogin,_id } = store.state.user  //获取当前登录用户的登录状态和id
      //有文章，有文章的作者，已登陆，这样写是因为可能因为异步的问题currentPost还未获取到值就执行了
      //所以需要用&&，当currentPost有值时在提取author属性
      if(currentPost.value && currentPost.value.author && isLogin){
        const postAuthor = currentPost.value.author as UserProps
        return postAuthor._id === _id   //判断当前文章的作者与当前登录的用户的id是否一样，一样才显示
      }else{
        return false 
      }
    })
    const currentImageUrl = computed(() => {  //如果文章有图,则返回宽度为850px的图片路径
      if (currentPost.value && currentPost.value.image) {
        const { image } = currentPost.value
        return (image as ImageProps).url + '?x-oss-process=image/resize,w_850'  //指定目标缩放图的宽度为850px
      } else {
        return null
      }
    })
    const hideAandDelete = () =>{ //在删除框中点击确定按钮，触发此事件
      modalIsVisible.value = false  //这句代码功能是删除框消失
      store.dispatch('deletePost',currentId).then((rawData:ResponseType<PostProps>)=>{
         createMessage('删除成功,1秒后跳转到专栏首页','success',1000)
         setTimeout(()=>{
           //一秒后跳转到专栏详情页
           router.push({name:'column',params:{id:rawData.data.column}}) 
         },1000)
      })
    }
    return {
      currentPost,
      currentImageUrl,
      currentHTML,
      showEditArea,
      modalIsVisible,
      hideAandDelete
    }
  }
})
</script>
